package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;
import com.xgkj.diyiketang.http.engine.TaskResultCode;

/**
 * Created by windo on 2015/11/24.
 */
public class RegisterTask extends CouponHttpBaseTask {
    private String mPhone;
    private String mPwd;
    private String mPwdAgain;
    private String mCode;
    private String mNickName;
    private String mSystem;
    private String mAreaName;
    private String mChannel;
    private static final String PHONE = "phoneNum";
    private static final String PWD = "pwd";
    private static final String VFCODE = "vfCode";
    private static final String NICKNAME = "nickName";
    private static final String SEX = "sex";
    private String mSex;

    public RegisterTask(String phone, String pwd, String code ,String nickname,String sex,String system,String areaName,String generalizeChannel) {
        super(TaskType.REGISTER_TASK);
        mPhone = phone;
        mPwd = pwd;
        mCode = code;
        mNickName = nickname;
        mSex = sex;
        mSystem = system;
        mAreaName =areaName;
        mChannel = generalizeChannel;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject json = response.getBody();
        EatShowUserInfo userInfo = null;
        if (json != null) {
            userInfo = JSON.parseObject(json.toString(), EatShowUserInfo.class);
        }
        notifyTaskCompleted(TaskResultCode.SUCCESS, userInfo);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT) {
            CouponHttpRequest request = new CouponHttpRequest(
                    RequestType.REGISTER_REQUEST);
            JSONObject json = new JSONObject();
            json.put(PHONE, mPhone);
            json.put(PWD, mPwd);
            json.put(VFCODE, mCode);
            json.put(NICKNAME, mNickName);
            json.put(SEX,mSex);
            json.put("system",mSystem);
            json.put("areaName",mAreaName);
            json.put("generalizeChannel",mChannel);
            request.setData(json.toString());
            request.setApiNo(ApiNo.REGISTER);
            addRequest(request);
        }

    }
}
